#include "interface.h"

#include <QtGui\QGridLayout>
#include <QtGui\QGroupBox>
#include <QtGui\QMessageBox>

const double SolverDialog::unitsVals[UNITS_NUMBER] = {1.0, 60.0, 3600.0, 86400.0, 604800.0, 2419200.0, 31536000.0};
const QString SolverDialog::unitsNames[UNITS_NUMBER] = {"Seconds", "Minutes", "Hours", "Earth Days", "Earth Weeks", "Earth Monthes (4 weeks)", "Earth Years (365 days)"};

SolverDialog::SolverDialog(QWidget* parent /* = NULL*/, const SolutionParameters& solutionParameters /*= SolutionParameters()*/)
  : QDialog(parent)
  , solPar(solutionParameters)
  , unitsType(SECONDS)
{
  setWindowTitle("Solution parameters");
  solversComboBox = new QComboBox(this);
  for (SolverType i = TEST_SOLVER; i < MAX_TYPE; ++i)
    solversComboBox->addItem(solversNames[i]);
  solversComboBox->setCurrentIndex(solPar.type);

  QGroupBox* timeGroupBox = new QGroupBox("Time paremters", this);
  QGridLayout* timeGroupLayout = new QGridLayout();

  timeUnitsComboBox = new QComboBox(this);
  for (int i = 0; i < UNITS_NUMBER; ++i)
    timeUnitsComboBox->addItem(unitsNames[i]);
  timeUnitsComboBox->setCurrentIndex(unitsType);
  connect(timeUnitsComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(changedTimeUnits(int)));

  QGroupBox* startGroupBox = new QGroupBox("Start time", this);
  QGridLayout* startLayout = new QGridLayout();
  startEdit = new RealNumTextEdit(solPar.tStart);
  startLayout->addWidget(startEdit);
  startGroupBox->setLayout(startLayout);
  
  QGroupBox* endGroupBox = new QGroupBox("End time", this);
  QGridLayout* endLayout = new QGridLayout();
  endEdit = new RealNumTextEdit(solPar.tEnd);
  endLayout->addWidget(endEdit);
  endGroupBox->setLayout(endLayout);

  QGroupBox* deltaGroupBox = new QGroupBox("Delta time", this);
  QGridLayout* deltaLayout = new QGridLayout();
  deltaEdit = new RealNumTextEdit(solPar.tDelta);
  deltaLayout->addWidget(deltaEdit);
  deltaGroupBox->setLayout(deltaLayout);

  connect(startEdit, SIGNAL(enterPressed()), endEdit, SLOT(setFocus()));
  connect(startEdit, SIGNAL(enterPressed()), endEdit, SLOT(selectAll()));

  connect(endEdit, SIGNAL(enterPressed()), deltaEdit, SLOT(setFocus()));
  connect(endEdit, SIGNAL(enterPressed()), deltaEdit, SLOT(selectAll()));

  timeGroupLayout->addWidget(timeUnitsComboBox, 0, 0, 1, 3);
  timeGroupLayout->addWidget(startGroupBox, 1, 0, 1, 3);
  timeGroupLayout->addWidget(endGroupBox, 2, 0, 1, 3);
  timeGroupLayout->addWidget(deltaGroupBox, 3, 0, 1, 3);
  timeGroupBox->setLayout(timeGroupLayout);

  solveButton = new QPushButton("Solve", this);
  connect(deltaEdit, SIGNAL(enterPressed()), solveButton, SLOT(click()));
  connect(solveButton, SIGNAL(clicked()), this, SLOT(solve()));
  cancelButton = new QPushButton("Cancel", this);
  connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancel()));

  QGridLayout* pLayout = new QGridLayout();
  pLayout->addWidget(solversComboBox, 0, 0, 1, 3);
  pLayout->addWidget(timeGroupBox, 1, 0, 1, 3);
  pLayout->addWidget(solveButton, 5, 0, 1, 1);
  pLayout->addWidget(cancelButton, 5, 2, 1, 1);
  setLayout(pLayout);
}

SolverDialog::~SolverDialog()
{
}

void SolverDialog::cancel()
{
  hide();
}
void SolverDialog::solve()
{
  bool isStartSolution = true, ok;
  solPar.tStart = startEdit->toPlainText().toDouble(&ok) * unitsVals[unitsType];
  isStartSolution &= ok;
  solPar.tEnd = endEdit->toPlainText().toDouble(&ok) * unitsVals[unitsType];
  isStartSolution &= ok;
  solPar.tDelta = deltaEdit->toPlainText().toDouble(&ok) * unitsVals[unitsType];
  isStartSolution &= ok;

  solPar.type = (SolverType)solversComboBox->currentIndex();
  if (isStartSolution)
  {
    hide();
    emit startSolution(solPar);
  }
  else
  {
    QMessageBox* errorMessageBox = new QMessageBox(QMessageBox::Critical,"Error", "Invalid input data!", QMessageBox::Close);
    errorMessageBox->show();
  }
}
void SolverDialog::changedTimeUnits(int idx)
{
  double val;
  bool ok;
  val = startEdit->toPlainText().toDouble(&ok) * unitsVals[unitsType];
  if (ok)
    startEdit->setText(QString::number(val / unitsVals[idx]));
  val = endEdit->toPlainText().toDouble(&ok) * unitsVals[unitsType];
  if (ok)
    endEdit->setText(QString::number(val / unitsVals[idx]));
  val = deltaEdit->toPlainText().toDouble(&ok) * unitsVals[unitsType];
  if (ok)
    deltaEdit->setText(QString::number(val / unitsVals[idx]));
  
  unitsType = (TimeUnits)idx;
}


